<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Dialogs</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Winforms, Mono, C#, dialogs, programming, tutorial">
<meta name="description" content="Dialogs">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href=".." title="Home">Contents</a>


<h1>Dialogs</h1>


<p>
In this part of the Mono Winforms tutorial, we will talk about dialogs.
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

<p>
Dialog windows or dialogs are an indispensable part of most modern GUI 
applications. A dialog is defined as a conversation between two or more 
persons. In a computer application a dialog is a window which is used to 
"talk" to the application. A dialog is used to input data, modify data, 
change the application settings etc. Dialogs are important means of 
communication between a user and a computer program.
</p>

<p>
There are essentially two types of dialogs. Predefined dialogs and custom dialogs. 
</p>


<h2>FolderBrowserDialog</h2>

<p>
This dialog prompts the user to select a folder. 
</p>

<div class="codehead">folderbrowserdialog.cs</div>
<pre class="code">
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {

    private ToolBar toolbar;
    private ToolBarButton open;
    private StatusBar statusbar;

    public MForm() {
        Text = "FolderBrowserDialog";
 
        toolbar = new ToolBar();
        open = new ToolBarButton();

        statusbar = new StatusBar();
        statusbar.Parent = this;

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        Controls.Add(toolbar);

        CenterToScreen();
    }


    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       FolderBrowserDialog dialog = new FolderBrowserDialog();

       if (dialog.ShowDialog(this) == DialogResult.OK) {
           statusbar.Text = dialog.SelectedPath;
       }
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}
</pre>

<p>
We have a toolbar and one toolbar button. By clicking on the button, 
the <b class="keyword">FolderBrowserDialog</b> appears on the screen. 
The name of the selected folder is shown in the statusbar. 
</p>


<pre class="explanation">
 FolderBrowserDialog dialog = new FolderBrowserDialog();
</pre>

<p>
The <b class="keyword">FolderBrowserDialog</b> is created.
</p>

<pre class="explanation">
 if (dialog.ShowDialog(this) == DialogResult.OK) {
     statusbar.Text = dialog.SelectedPath;
 }       
</pre>

<p>
The <b class="keyword">ShowDialog()</b> method shows the dialog on the screen. 
If we click on the OK button of the dialog, the selected directory path is 
shown on the statusbar.
</p>


<br>
<img src="/img/gui/sharpwinf/folderbrowser.png" alt="FolderBrowserDialog">
<div class="figure">Figure: FolderBrowserDialog</div>


<h2>ColorDialog</h2>

<p>
This dialog displays available colors along with controls that enable 
the user to define custom colors. 
</p>

<div class="codehead">colordialog.cs</div>
<pre class="code">
 using System;
 using System.Drawing;
 using System.Windows.Forms;

class MForm : Form {

    private ToolBar toolbar;
    private ToolBarButton open;
    private Color color;

    private int rectWidth = 100;
    private int rectHeight = 100;
    private Rectangle r;

    public MForm() {
        Text = "ColorDialog";

        color = Color.Blue;
 
        toolbar = new ToolBar();
        open = new ToolBarButton();

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        LocateRect();

        SetStyle (ControlStyles.ResizeRedraw, true);
        Controls.Add(toolbar);
        Paint += new PaintEventHandler(OnPaint);

        CenterToScreen();
    }


    void OnPaint(object sender, PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      LocateRect();

      SolidBrush brush = new SolidBrush(color);

      g.FillRectangle(brush, r);
    }


    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       ColorDialog dialog = new ColorDialog();
       if (dialog.ShowDialog(this) == DialogResult.OK) {
          color = dialog.Color;
          Invalidate();
       }
    }

    void LocateRect() {
        int x = (ClientSize.Width - rectWidth) / 2;
        int y = (ClientSize.Height - rectHeight) / 2;
        r = new Rectangle(x, y, rectWidth, rectHeight);
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}
</pre>

<p>
In this code example, we use the <b class="keyword">ColorDialog</b> 
to choose a color for a rectangle, that is
located in the middle of the form control. 
</p>

<pre class="explanation">
 color = Color.Blue;
</pre>

<p>
At the beginning, the color of the rectangle is blue. We use the 
<b class="keyword">color</b> variable to determine
the color of the rectangle. 
</p>

<pre class="explanation">
 ColorDialog dialog = new ColorDialog();
</pre>

<p>
The <b class="keyword">ColorDialog</b> is created. 
</p>

<pre class="explanation">
 if (dialog.ShowDialog(this) == DialogResult.OK) {
    color = dialog.Color;
    Invalidate();
 }
</pre>

<p>
The code shows the color dialog. If we click on the OK button, we get 
the selected color and call the <b class="keyword">Invalidate()</b> method. 
The method invalidates the entire surface of the control and causes the 
control to be redrawn. The result is that the rectangle is drawn with a 
new color value.
</p>


<br>
<img src="/img/gui/sharpwinf/colordialog.png" alt="ColorDialog">
<div class="figure">Figure: ColorDialog</div>


<h2>FontDialog</h2>

<p>
The <b class="keyword">FontDialog</b> is used to select fonts.
</p>

<div class="codehead">fontdialog.cs</div>
<pre class="code">
 using System;
 using System.Drawing;
 using System.Windows.Forms;

class MForm : Form {

    private Label text;
    private ToolBar toolbar;
    private ToolBarButton open;

    public MForm() {
        Text = "FontDialog";

        text = new Label();
        text.Parent = this;
        text.Text = "Winforms tutorial";

        LocateText();
 
        toolbar = new ToolBar();
        toolbar.Parent = this;
        open = new ToolBarButton();

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        text.AutoSize = true;
        Resize += new EventHandler(OnResize);

        CenterToScreen();
    }

    void OnResize(object sender, EventArgs e){
       LocateText();
    }

    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       FontDialog dialog = new FontDialog();
       if (dialog.ShowDialog(this) == DialogResult.OK) {
          text.Font = dialog.Font;
          LocateText();
       }
    }

    void LocateText() {
        text.Top = (this.ClientSize.Height - text.Height) / 2;
        text.Left = (this.ClientSize.Width - text.Width) / 2;
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}
</pre>

<p>
We draw some text in the middle of the form control. We use the font 
dialog to change font for this text. 
</p>

<pre class="explanation">
 FontDialog dialog = new FontDialog();
</pre>

<p>
<b class="keyword">FontDialog</b> is created. 
</p>

<pre class="explanation">
 if (dialog.ShowDialog(this) == DialogResult.OK) {
    text.Font = dialog.Font;
    LocateText();
 }
</pre>

<p>
When we click on the OK button, we set a newly selected font for the 
<b class="keyword">Label</b> control. Because the size of the text 
changes with different fonts, we must call the <b class="keyword">LocateText()</b> 
method, which locates the text in the middle of the form control. 
</p>

<br>
<img src="/img/gui/sharpwinf/fontdialog.png" alt="FontDialog">
<div class="figure">Figure: FontDialog</div>


<h2>OpenDialog</h2>

<p>
This dialog is used to open files.
</p>

<div class="codehead">opendialog.cs</div>
<pre class="code">
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {

    private ToolBar toolbar;
    private ToolBarButton open;
    private TextBox textbox;

    public MForm() {
        Text = "OpenFileDialog";

        toolbar = new ToolBar();
        open = new ToolBarButton();

        textbox = new TextBox();
        textbox.Multiline = true;
        textbox.ScrollBars = ScrollBars.Both;
        textbox.WordWrap = false;
        textbox.Parent = this;
        textbox.Dock = DockStyle.Fill;
       

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        Controls.Add(toolbar);
        Controls.Add(textbox);

 	CenterToScreen();
    }

    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       OpenFileDialog dialog = new OpenFileDialog();
       dialog.Filter = "C# files (*.cs)|*.cs";

       if (dialog.ShowDialog(this) == DialogResult.OK) {
           StreamReader reader = new StreamReader(dialog.FileName);
           string data = reader.ReadToEnd();
           reader.Close();
           textbox.Text = data;
       }
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}
</pre>

<p>
We use the <b class="keyword">OpenDialog</b> control to open C# source files. 
We have a <b class="keyword">TextBox</b> control, where we display the file. 
</p>


<pre class="explanation">
 OpenFileDialog dialog = new OpenFileDialog();
</pre>

<p>
The <b class="keyword">OpenDialog</b> is created. 
</p>

<pre class="explanation">
 dialog.Filter = "C# files (*.cs)|*.cs";
</pre>

<p>
We set the <b class="keyword">Filter</b> property to C# source files. 
Only C# files can be chosen with this dialog instance. 
</p>

<pre class="explanation">
 if (dialog.ShowDialog(this) == DialogResult.OK) {
     StreamReader reader = new StreamReader(dialog.FileName);
     string data = reader.ReadToEnd();
     reader.Close();
     textbox.Text = data;
 }
</pre>

<p>
After clicking OK, we read the contents of the chosen file and put it into 
the <b class="keyword">TextBox</b> control. 
</p>

<br>
<img src="/img/gui/sharpwinf/opendialog.png" alt="OpenDialog">
<div class="figure">Figure: OpenDialog</div>

<hr class="btm">

<p>
In this part of the Mono Winforms tutorial, we showed various dialogs. 
</p>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified December 27, 2008  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- content -->

</body>
</html>

